﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class PStock : BasePage
    {
        IStockBLL stockBLL = (IStockBLL)IocContainer.Instance.Resolve<IStockBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                //ControlAuthority(new List<LinkButton> { btnAdd, btnUpdate, btnDelete }, 3);
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                StringBuilder strs = new StringBuilder();
                string strWhere = "";
                if (!string.IsNullOrEmpty(this.txtStockCode.Text.Trim()))
                    strWhere += string.Format(" and FNumber like '%{0}%'", this.txtStockCode.Text.Trim());
                if (!string.IsNullOrEmpty(this.txtStockName.Text.Trim()))
                    strWhere += string.Format(" and FName like '%{0}%'", this.txtStockName.Text.Trim());
                if (this.ddlstType.SelectedValue != "0")
                    strWhere += string.Format(" and FType like '%{0}%'", this.ddlstType.SelectedValue);
                if (this.ddlstStatus.SelectedValue != "0")
                    strWhere += string.Format(" and FStatus like '%{0}%'", this.ddlstStatus.SelectedValue);
                DataTable dt = stockBLL.Query(strWhere);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        string strStatus = dataRow["FStatus"].ToString();
                        switch(strStatus)
                        {
                            case "1":
                                strStatus = "确认";
                                break;
                            case "2":
                                strStatus = "部分关闭";
                                break;
                            case "3":
                                strStatus = "关闭";
                                break;
                        }
                        strs.AppendFormat("<tr rel='{0}'>", dataRow["FitemId"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FNumber"], dataRow["FName"], dataRow["TypeName"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["DepartmentName"], strStatus, dataRow["FDree"]);
                        strs.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", dataRow["FFrelation"], dataRow["FSrelation"], dataRow["FTel"]);
                        strs.AppendFormat("<td>{0}</td></tr>", dataRow["FMobil"]);
                    }
                }
                litStockList.Text = strs.ToString();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int Id = 0;
                int.TryParse(this.selectedStockID.Value.Trim(), out Id);
                stockBLL.Delete(Id);
                LoadData();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
            }

        }
    }
}